//
//  XNBaseBillInfoViewController.m
//  XINNingOA
//
//  Created by samuel on 2021/7/12.
//

#import "XNBaseBillInfoViewController.h"
#import "XNCheckPassRequest.h"
#import "XNDismissStepRequest.h"
#import "XNCompleteDismissRequest.h"
#import "XNSubTableViewController.h"
#import "XNBillListRequest.h"
#import "XNUtil.h"

@interface XNBaseBillInfoViewController ()<XNApprovePopViewDelegate>

@end

@implementation XNBaseBillInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backItemClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backItem;
    
    self.showArray = [NSMutableArray array];
    
    [self creatBottomBtn];
    
    UITableView *tableView = [[UITableView alloc] init];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
    tableView.estimatedRowHeight = 100;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.bottom.mas_equalTo(self.view).offset(is_iPhoneXSerious ? -80 : -50);
    }];
    

}

- (void)viewDidLoadWithoutBottomBtn
{
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backItemClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backItem;
    
    self.showArray = [NSMutableArray array];
    
    
    UITableView *tableView = [[UITableView alloc] init];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
    tableView.estimatedRowHeight = 100;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.bottom.mas_equalTo(self.view).offset(is_iPhoneXSerious ? -80 : -50);
    }];
}

- (void)creatBottomBtn
{
//    UIButton *dismissStepBtn = [[UIButton alloc] init];
//    dismissStepBtn.backgroundColor = [UIColor colorWithHexString:@"#DF912E"];
//    [dismissStepBtn setTitle:@"逐级驳回" forState:UIControlStateNormal];
//    [dismissStepBtn addTarget:self action:@selector(dismissStep:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:dismissStepBtn];
//    [dismissStepBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self.view);
//        make.height.mas_equalTo(is_iPhoneXSerious ? 80 : 50);
//        make.bottom.equalTo(self.view);
//        make.width.mas_equalTo(SCREENW/3);
//    }];
    
    UIButton *applyBtn = [[UIButton alloc] init];
    applyBtn.backgroundColor = [UIColor colorWithHexString:@"#58BA2C"];
    [applyBtn setTitle:@"审批通过" forState:UIControlStateNormal];
    [applyBtn addTarget:self action:@selector(apply:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:applyBtn];
    [applyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.height.mas_equalTo(is_iPhoneXSerious ? 80 : 50);
        make.bottom.equalTo(self.view);
        make.width.mas_equalTo(SCREENW/2);
    }];
    
    
    UIButton *completeDismissBtn = [[UIButton alloc] init];
    completeDismissBtn.backgroundColor = [UIColor colorWithHexString:@"#EF5459"];
    [completeDismissBtn setTitle:@"彻底驳回" forState:UIControlStateNormal];
    [completeDismissBtn addTarget:self action:@selector(completeDismiss:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:completeDismissBtn];
    [completeDismissBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view);
        make.height.mas_equalTo(is_iPhoneXSerious ? 80 : 50);
        make.bottom.equalTo(self.view);
        make.width.mas_equalTo(SCREENW/2);
    }];
}

- (void)backItemClick:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dismissStep:(UIButton *)sender
{
    self.popView = [[XNApprovePopView alloc] init];
    self.popView.showPicker = NO;
    self.popView.delegate = self;
    self.popView.approveType = XNApproveType_DismissStep;
    [self.popView show];
}

- (void)apply:(UIButton *)sender
{
    self.popView = [[XNApprovePopView alloc] init];
    self.popView.showPicker = NO;
    self.popView.delegate = self;
    self.popView.approveType = XNApproveType_Pass;
    [self.popView show];
}

- (void)completeDismiss:(UIButton *)sender
{
    self.popView = [[XNApprovePopView alloc] init];
    self.popView.showPicker = NO;
    self.popView.delegate = self;
    self.popView.approveType = XNApproveType_CompleteDismiss;
    [self.popView show];
}



- (void)PopView:(XNApprovePopView *)popView checkMessage:(NSString *)checkMessage ApproveType:(XNApproveType)approveType
{
    [SVProgressHUD show];
    if (approveType == XNApproveType_Pass) {
        XNCheckPassRequest *request = [[XNCheckPassRequest alloc] initWithOrderId:self.orderId Remark:checkMessage];
        [request startWithCompletionBlockWithSuccess:^(__kindof XNCheckPassRequest * _Nonnull request) {
            [SVProgressHUD dismiss];
            if (request.success) {
//                [self.navigationController  popViewControllerAnimated:YES];
                [self jumpNextBill];
                [XNHud showToastWithText:@"提交成功"];
            }
        } failure:^(__kindof XNCheckPassRequest * _Nonnull request) {
            [SVProgressHUD dismiss];
            [XNHud showToastWithText:@"请求失败"];
        }];
    }else if(approveType == XNApproveType_DismissStep){
        XNDismissStepRequest *request = [[XNDismissStepRequest alloc] initWithOrderId:self.orderId Remark:checkMessage];
        [request startWithCompletionBlockWithSuccess:^(__kindof XNDismissStepRequest * _Nonnull request) {
            [SVProgressHUD dismiss];
            if (request.success) {
                [self.navigationController  popViewControllerAnimated:YES];
                [XNHud showToastWithText:@"提交成功"];
            }
        } failure:^(__kindof XNDismissStepRequest * _Nonnull request) {
            [SVProgressHUD dismiss];
            [XNHud showToastWithText:@"请求失败"];
        }];
    }else if(approveType == XNApproveType_CompleteDismiss){
        XNCompleteDismissRequest *request = [[XNCompleteDismissRequest alloc] initWithOrderId:self.orderId Remark:checkMessage];
        [request startWithCompletionBlockWithSuccess:^(__kindof XNCompleteDismissRequest * _Nonnull request) {
            [SVProgressHUD dismiss];
            if (request.success) {
//                [self.navigationController  popViewControllerAnimated:YES];
                [self jumpNextBill];
                [XNHud showToastWithText:@"提交成功"];
            }
        } failure:^(__kindof XNCompleteDismissRequest * _Nonnull request) {
            [SVProgressHUD dismiss];
            [XNHud showToastWithText:@"请求失败"];
        }];
    }
}


- (void)jumpNextBill
{
    XNBillListRequest *listRequest = [[XNBillListRequest alloc] init];
    [listRequest startWithCompletionBlockWithSuccess:^(__kindof XNBillListRequest * _Nonnull request) {
        NSArray *array = request.billList;
        if (array.count > 0) {
            XNBillItemModel *item = array[0];
            XNBaseBillInfoViewController *vc = [XNUtil getControllerWithOrderId:item.orderId];
            vc.title = item.billName;
            NSMutableArray *viewCtrs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
            NSInteger index = [viewCtrs indexOfObject:self];
            [viewCtrs removeObjectAtIndex:index];
             [viewCtrs addObject:vc];
             [self.navigationController setViewControllers:viewCtrs animated:YES];

            
        }else{
            [self.navigationController  popViewControllerAnimated:YES];
        }
    } failure:^(__kindof XNBillListRequest * _Nonnull request) {
        [self.navigationController  popViewControllerAnimated:YES];
    }];
}


#pragma mark Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.showArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    XNTableShowDataModel *dataModel = self.showArray[section];
    return dataModel.showDataArray.count;

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    XNTableShowDataModel *dataModel = self.showArray[section];
    XNItemHeadView *headView = [[XNItemHeadView alloc] initWithTitle:dataModel.title];
    headView.showWarning = dataModel.showWarning;
    headView.warningTitle = dataModel.warningStr;
    headView.totalArray = dataModel.headTitleArray;
    return headView;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XNTableShowDataModel *dataModel = self.showArray[indexPath.section];
    XNShowItemModel *itemModel = dataModel.showDataArray[indexPath.row];
    if (dataModel.showType == XNShowType_Approve) {
        XNApproveTableViewCell *cell = [XNApproveTableViewCell tabelViewCellWithTableView:tableView ForIndexPath:indexPath];
        cell.approveModel = itemModel.showItemArray[0];
        return cell;
    }else if(dataModel.showType == XNShowType_MoreData){
        XNMoreDataTableViewCell *cell = [XNMoreDataTableViewCell tabelViewCellWithTableView:tableView ForIndexPath:indexPath];
        XNTableShowDataModel *itemDataModel = dataModel.showDataArray[indexPath.row];
        cell.title = itemDataModel.title;
        return cell;
    }else{
        XNNormalTableViewCell *cell = [XNNormalTableViewCell tabelViewCellWithTableView:tableView ForIndexPath:indexPath];
        cell.itemModel = itemModel;
//        cell.showInfoArray = itemModel.showItemArray;
//        cell.showOverImage = itemModel.showOverImage;
        return cell;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    XNTableShowDataModel *dataModel = self.showArray[indexPath.section];
    if (dataModel.showType == XNShowType_MoreData) {
        XNTableShowDataModel *subData = dataModel.showDataArray[indexPath.row];
        XNSubTableViewController  *subVC = [[XNSubTableViewController alloc] init];
        subVC.title = subData.title;
        subVC.tableShowModel = subData;
        [self.navigationController pushViewController:subVC animated:YES];
        
    }

}

@end
